package com.umeox.watch.moto.dataservice;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import com.umeox.moto.common.log.Logger;
import com.umeox.moto.common.utils.DateUtils;
import com.umeox.watch.moto.dataservice.constants.Constants;
import com.umeox.watch.moto.dataservice.db.model.BuddyInfo;
import com.umeox.watch.moto.dataservice.db.model.ContactInfo;
import com.umeox.watch.moto.dataservice.db.model.IMGroup;
import com.umeox.watch.moto.dataservice.db.model.IMGroupMember;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("Range")
public class DataProvider {

    private static final String TAG = DataProvider.class.getSimpleName();
    private static final Uri BASE_CONTENT_URI = Uri.parse("content://" + BuildConfig.AUTHORITY);
    private static Context context;

    public static void initialize(Context context) {
        DataProvider.context = context;
    }

    private static Context getContext() {
        return context;
    }

    /**
     * 判断设备是否已经绑定
     */
    public static boolean isActivated() {
        return getBoolean(DeviceConfigProvider.getUri(), DeviceConfigProvider.ACTIVE_STATUS, false);
    }

    /**
     * 获取当前手表设置的主题
     */
    public static int getWallpaper() {
        return getInt(DeviceConfigProvider.getUri(), DeviceConfigProvider.WALLPAPER, 0);
    }

    /**
     * 获取用户设定的自定义壁纸
     */
    public static String getCustomWallpaper() {
        return getString(DeviceConfigProvider.getUri(), DeviceConfigProvider.CUSTOM_WALLPAPER);
    }

    /**
     * 设置手表主题
     */
    public static boolean setWallpaper(int index) {
        ContentValues values = new ContentValues();
        values.put(DeviceConfigProvider.WALLPAPER, index);
        int effect = getContext().getContentResolver().update(DeviceConfigProvider.getUri(), values, null, null);
        return effect > 0;
    }

    /**
     * 听过friendId 查询hiddenFeatures
     */
    public static String getHiddenFeaturesByFriendId(String friendId) {
        String result = "";
        Cursor mCursor = null;
        try {
            String selection = BuddyProvider.FRIEND_ID + "=? ";
            String[] selectionArgs = new String[]{friendId};
            mCursor = getContext().getContentResolver().query(BuddyProvider.getUri(), null, selection, selectionArgs, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                result = mCursor.getString(mCursor.getColumnIndex(BuddyProvider.HIDDEN_FEATURES));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }

    /**
     * 设置自定义壁纸
     */
    public static boolean setCustomWallpaper(String path) {
        ContentValues values = new ContentValues();
        values.put(DeviceConfigProvider.CUSTOM_WALLPAPER, path);
        int effect = getContext().getContentResolver().update(DeviceConfigProvider.getUri(), values, null, null);
        return effect > 0;
    }

    /**
     * 删除语聊好友
     */
    public static int removeAllChatContact() {
        return getContext().getContentResolver().delete(ChatContactProvider.getUri(), null, null);
    }

    /**
     * 获取长度单位
     */
    public static int getDistanceUnit() {

        final Cursor cursor = getContext().getContentResolver().query(SettingsProvider.SETTINGS_CONTENT_URI, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                return cursor.getInt(cursor.getColumnIndex(SettingsProvider.DISTANCE_UNIT));
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return 0;
    }

    /**
     * 判断设备是否启用自动接听功能
     */
    public static boolean isAutoAnswer() {
        return getBoolean(DeviceConfigProvider.getUri(), DeviceConfigProvider.AUTO_ANSWER, false);
    }

    /**
     * 判断设备是否报告通话位置
     */
    public static boolean isReportCallLocation() {
        return getBoolean(DeviceConfigProvider.getUri(), DeviceConfigProvider.REPORT_CALL_LOCATION, false);
    }

    /**
     * 判断设备是否报告通话位置
     */
    public static boolean isReportShutdownLocation() {
        return getBoolean(DeviceConfigProvider.getUri(), DeviceConfigProvider.REPORT_SHUTDOWN_LOCATION, false);
    }

    /**
     * 判断手表是否被服务端禁止关机功能
     */
    public static boolean isShutdownProhibited() {
        return getBoolean(DeviceConfigProvider.getUri(), DeviceConfigProvider.FORBIDDEN_SHUTDOWN, false);
    }

    /**
     * 判断是否处于禁用时段
     */
    public static boolean inForbidPeriodNow() {
        return getBoolean(DeviceConfigProvider.getUri(), DeviceConfigProvider.IN_FORBIDPERIOD_FLAG, false);
    }


    public static String getQrCode() {
        return getString(DeviceCodeProvider.getUri(), DeviceCodeProvider.QR_CODE);
    }

    public static String getBindCode() {
        return getString(DeviceCodeProvider.getUri(), DeviceCodeProvider.BIND_CODE);
    }


    /**
     * 判断手表是否开启拒绝陌生来电功能
     */
    public static boolean isRefusePhone() {
        return getBoolean(DeviceConfigProvider.getUri(), DeviceConfigProvider.WHITE_LIST, false);
    }

    /**
     * 获取佩戴者ID
     */
    public static String getHolderId() {
        return getString(HolderInfoProvider.getUri(), HolderInfoProvider.HOLDER_ID);
    }

    /**
     * 获取晡礼的算法
     */
    public static int getAsrJuristicMethod() {
        return getInt(PrayerSettingsProvider.getUri(), PrayerSettingsProvider.ASRJURISTIC_METHOD, 0);
    }

    /**
     * 获取祈祷提前多少分钟提醒
     */
    public static int getPrayerReminder() {
        return getInt(PrayerSettingsProvider.getUri(), PrayerSettingsProvider.PRAYER_REMINDER, 0);
    }

    /**
     * 获取惯例下标
     */
    public static int getPrayerIndex() {
        return getInt(PrayerSettingsProvider.getUri(), PrayerSettingsProvider.INDEX, 0);
    }


    /**
     * 获取惯例下标
     */
    public static String getAzan() {
        return getString(PrayerSettingsProvider.getUri(), PrayerSettingsProvider.AZAN);
    }


    /**
     * 判断设备是否已经进入Launcher
     */
    public static boolean isLaunched() {
        return getBoolean(DeviceConfigProvider.getUri(), DeviceConfigProvider.LAUNCHED, false);
    }

    public static int getSportsChallengeRewardsValue() {
        return getInt(HolderInfoProvider.getUri(), HolderInfoProvider.SPORTS_CHALLENGE_REWARDS, 0);
    }

    public static int getQuranChallengeRewardsValue() {
        return getInt(HolderInfoProvider.getUri(), HolderInfoProvider.QURAN_LEARN_TOTAL_REWARDVALUE, 0);
    }

    public static int getChantsCounterTotal() {
        return getInt(HolderInfoProvider.getUri(), HolderInfoProvider.CHANTS_COUNTER_TOTAL, 0);
    }

    /**
     * Launcher应用欢迎页面进入主页时调用该方法
     */
    @SuppressLint("UnsafeImplicitIntentLaunch")
    public static boolean launch() {
        ContentValues values = new ContentValues();
        values.put(DeviceConfigProvider.LAUNCHED, 1);
        boolean result = getContext().getContentResolver().update(DeviceConfigProvider.getUri(), values, null, null) > 0;
        if (result) {
            getContext().sendBroadcast(new Intent(Constants.ACTION_LAUNCHED));
        }
        return result;
    }

    /**
     * 开机或者关机调用改方法重置标记
     */
    public static void resetFlag() {
        ContentValues values = new ContentValues();
        values.put(DeviceConfigProvider.LAUNCHED, 0);
        getContext().getContentResolver().update(DeviceConfigProvider.getUri(), values, null, null);
    }

    /**
     * 获取当前手表设置的主题
     */
    public static int getCurrentTheme() {
        return getInt(DeviceConfigProvider.getUri(), DeviceConfigProvider.CURRENT_THEME, 0);
    }

    public static boolean isVideoCallForbidden() {
        return getBoolean(DataProvider.DeviceConfigProvider.getUri(), DeviceConfigProvider.FORBIDDEN_VIDEO_CALL, false);
    }

    /**
     * 标记手表禁用时段状态
     *
     * @param enable true表示正处于禁用时段，false反之
     */
    public static boolean inForbidPeriod(boolean enable) {
        ContentValues values = new ContentValues();
        values.put(DeviceConfigProvider.IN_FORBIDPERIOD_FLAG, enable ? 1 : 0);
        return getContext().getContentResolver().update(DeviceConfigProvider.getUri(), values, null, null) > 0;
    }

    /**
     * 获取当前手表定位频率值
     */
    public static int getCurrentFrequency() {
        return getInt(LocationPeriodProvider.getUri(), LocationPeriodProvider.FREQUENCY, 0);
    }

    /**
     * 设置手表主题
     */
    public static boolean setTheme(int theme) {
        ContentValues values = new ContentValues();
        values.put(DeviceConfigProvider.CURRENT_THEME, theme);
        int effect = getContext().getContentResolver().update(DeviceConfigProvider.getUri(), values, null, null);
        if (effect > 0) {
            return true;
        }
        return false;
    }

    public static boolean setWatchFace(int watchFace) {
        ContentValues values = new ContentValues();
        values.put(DeviceConfigProvider.WALLPAPER, watchFace);
        int effect = getContext().getContentResolver().update(DeviceConfigProvider.getUri(), values, null, null);
        return effect > 0;
    }

    public static int getWatchFace() {
        return getInt(DeviceConfigProvider.getUri(), DeviceConfigProvider.WALLPAPER, 0);
    }

    /**
     * 获取运动计划值
     *
     * @param val 默认值
     */
    public static int getPlanValue(int... val) {
        int def = 5000;
        if (val != null && val.length > 0) {
            def = val[0] <= 0 ? def : val[0];
        }
        int temp = getInt(HolderInfoProvider.getUri(), HolderInfoProvider.STEP_PLAN_VALUE, def);
        return temp <= 0 ? def : temp;
    }

    /**
     * 获取手表佩戴者昵称
     */
    public static String getNickname() {
        return getString(HolderInfoProvider.getUri(), HolderInfoProvider.NAME);
    }

    public static int getAudioTypes() {
        return getInt(DataProvider.DeviceConfigProvider.getUri(), DeviceConfigProvider.SCENE_MODE, 0);
    }

    /**
     * 获取定位方法 0：服务端定位，1：设备端高德定位
     *
     * @return
     */
    public static int getLocMethod() {
        return getInt(DataProvider.DeviceConfigProvider.getUri(), DeviceConfigProvider.LOC_METHOD, 1);
    }

    /**
     * 获取手表佩戴者头像
     */
    public static String getAvatar() {
        return getString(HolderInfoProvider.getUri(), HolderInfoProvider.AVATAR);
    }

    /**
     * 获取赞念数据
     */
    public static int getChantsCounterChallengeTotal() {
        return getInt(HolderInfoProvider.getUri(), HolderInfoProvider.CHANTS_COUNTER_CHALLENGE_TOTAL, 0);
    }

    /**
     * 获取群聊设置
     */
    public static boolean getCustomGroup() {
        return getInt(DeviceConfigProvider.getUri(), DeviceConfigProvider.CUSTOM_GROUP, 0) == 1;
    }

    /**
     * 获取AI开关
     */
    public static boolean getQimi() {
        return getInt(DeviceConfigProvider.getUri(), DeviceConfigProvider.QIMI, 0) == 1;
    }

    /**
     * 获取手表佩戴者身高，单位cm
     *
     * @param val 默认值
     */
    public static double getHeight(double... val) {
        double def = 167.1d;
        if (val != null && val.length > 0) {
            def = val[0] <= 0 ? def : val[0];
        }
        double temp = getDouble(HolderInfoProvider.getUri(), HolderInfoProvider.HEIGHT, def);
        return temp <= 0 ? def : temp;
    }

    /**
     * 获取手表佩戴者体重，单位kg
     *
     * @param val 默认值
     */
    public static double getWeight(double... val) {
        double def = 61.5d;
        if (val != null && val.length > 0) {
            def = val[0] <= 0 ? def : val[0];
        }
        double temp = getDouble(HolderInfoProvider.getUri(), HolderInfoProvider.WEIGHT, def);
        return temp <= 0 ? def : temp;
    }

    /**
     * 获取手表佩戴者信息和手表当前状态
     */
    public static ContentValues getDeviceConfig(String deviceId) {
        ContentValues result = null;
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(DeviceConfigProvider.getUri(), null, DeviceConfigProvider.DEVICE_ID + "=?", new String[]{deviceId}, null);
            if (mCursor != null && mCursor.moveToNext()) {
                result = new ContentValues();
                result.put(DeviceConfigProvider.DEVICE_ID, mCursor.getString(mCursor.getColumnIndex(DeviceConfigProvider.DEVICE_ID)));
                result.put(DeviceConfigProvider.ACTIVE_STATUS, mCursor.getInt(mCursor.getColumnIndex(DeviceConfigProvider.ACTIVE_STATUS)));
                result.put(DeviceConfigProvider.SIM_TYPE, mCursor.getInt(mCursor.getColumnIndex(DeviceConfigProvider.SIM_TYPE)));
                result.put(DeviceConfigProvider.CONTRACT_MACHINE, mCursor.getInt(mCursor.getColumnIndex(DeviceConfigProvider.CONTRACT_MACHINE)));
                result.put(DeviceConfigProvider.WHITE_LIST, mCursor.getInt(mCursor.getColumnIndex(DeviceConfigProvider.WHITE_LIST)));
                result.put(DeviceConfigProvider.OFF_ALARM, mCursor.getInt(mCursor.getColumnIndex(DeviceConfigProvider.OFF_ALARM)));
                result.put(DeviceConfigProvider.SCENE_MODE, mCursor.getInt(mCursor.getColumnIndex(DeviceConfigProvider.SCENE_MODE)));
                result.put(DeviceConfigProvider.FORBIDDEN_SHUTDOWN, mCursor.getInt(mCursor.getColumnIndex(DeviceConfigProvider.FORBIDDEN_SHUTDOWN)));
                result.put(DeviceConfigProvider.FORBIDDEN_VIDEO_CALL, mCursor.getInt(mCursor.getColumnIndex(DeviceConfigProvider.FORBIDDEN_VIDEO_CALL)));
                result.put(DeviceConfigProvider.CURRENT_THEME, mCursor.getInt(mCursor.getColumnIndex(DeviceConfigProvider.CURRENT_THEME)));
                result.put(DeviceConfigProvider.IN_FORBIDPERIOD_FLAG, mCursor.getInt(mCursor.getColumnIndex(DeviceConfigProvider.IN_FORBIDPERIOD_FLAG)));
                result.put(DeviceConfigProvider.WALLPAPER, mCursor.getInt(mCursor.getColumnIndex(DeviceConfigProvider.WALLPAPER)));
                result.put(DeviceConfigProvider.AUTO_ANSWER, mCursor.getInt(mCursor.getColumnIndex(DeviceConfigProvider.AUTO_ANSWER)));
                result.put(DeviceConfigProvider.LOC_METHOD, mCursor.getInt(mCursor.getColumnIndex(DeviceConfigProvider.LOC_METHOD)));
                mCursor.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }


    /**
     * 获取手表二维码信息
     */
    public static ContentValues getDeviceCode(String deviceId) {
        ContentValues result = null;
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(DeviceCodeProvider.getUri(), null, DeviceCodeProvider.DEVICE_ID + "=?", new String[]{deviceId}, null);
            if (mCursor != null && mCursor.moveToNext()) {
                result = new ContentValues();
                result.put(DeviceCodeProvider.DEVICE_ID, mCursor.getString(mCursor.getColumnIndex(DeviceCodeProvider.DEVICE_ID)));
                result.put(DeviceCodeProvider.QR_CODE, mCursor.getString(mCursor.getColumnIndex(DeviceCodeProvider.QR_CODE)));
                result.put(DeviceCodeProvider.BIND_CODE, mCursor.getString(mCursor.getColumnIndex(DeviceCodeProvider.BIND_CODE)));
                result.put(DeviceCodeProvider.EXPIRED_TIME, mCursor.getLong(mCursor.getColumnIndex(DeviceCodeProvider.EXPIRED_TIME)));
                mCursor.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }

    /**
     * 保存手表佩戴者信息和手表当前状态
     */
    public static long saveDeviceConfig(ContentValues values) {
        Uri uriWithAppendedId = getContext().getContentResolver().insert(DeviceConfigProvider.getUri(), values);
        try {
            return Long.parseLong(uriWithAppendedId.getLastPathSegment());
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 获取所有闹钟
     */
    public static List<ContentValues> getAlarms() {
        List<ContentValues> result = null;
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(AlarmProvider.getUri(), null, null, null, null);
            if (mCursor != null && mCursor.getCount() > 0) {
                result = new ArrayList<>(mCursor.getCount());
                int index = 0;
                while (mCursor.moveToNext()) {
                    ContentValues data = new ContentValues();
                    data.put(AlarmProvider.ID, mCursor.getLong(mCursor.getColumnIndex(AlarmProvider.ID)));
                    data.put(AlarmProvider.ALARM_ID, mCursor.getLong(mCursor.getColumnIndex(AlarmProvider.ALARM_ID)));
                    data.put(AlarmProvider.ALARM_NAME, mCursor.getString(mCursor.getColumnIndex(AlarmProvider.ALARM_NAME)));
                    data.put(AlarmProvider.ALARM_TIME, mCursor.getString(mCursor.getColumnIndex(AlarmProvider.ALARM_TIME)));
                    data.put(AlarmProvider.REPEAT_FLAG, mCursor.getInt(mCursor.getColumnIndex(AlarmProvider.REPEAT_FLAG)));
                    data.put(AlarmProvider.REPEAT_EXPRESSION, mCursor.getString(mCursor.getColumnIndex(AlarmProvider.REPEAT_EXPRESSION)));
                    data.put(AlarmProvider.RING_TYPE, mCursor.getInt(mCursor.getColumnIndex(AlarmProvider.RING_TYPE)));
                    data.put(AlarmProvider.ALARM_STATE, mCursor.getInt(mCursor.getColumnIndex(AlarmProvider.ALARM_STATE)));
                    data.put(AlarmProvider.RING_SRC, mCursor.getString(mCursor.getColumnIndex(AlarmProvider.RING_SRC)));
                    data.put(AlarmProvider.ALARM_TIMEINMILLIS, mCursor.getLong(mCursor.getColumnIndex(AlarmProvider.ALARM_TIMEINMILLIS)));
                    result.add(index, data);
                    index++;
                }
                mCursor.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }

    /**
     * 根据条件获取闹钟
     */
    public static ContentValues getAlarm(long id) {
        ContentValues data = null;
        Cursor mCursor = null;
        try {
            String selection = AlarmProvider.ID + "=?";
            String[] selectionArgs = new String[]{String.valueOf(id)};
            mCursor = getContext().getContentResolver().query(AlarmProvider.getUri(), null, selection, selectionArgs, null);
            if (mCursor != null && mCursor.moveToNext()) {
                data = new ContentValues();
                data.put(AlarmProvider.ID, mCursor.getLong(mCursor.getColumnIndex(AlarmProvider.ID)));
                data.put(AlarmProvider.ALARM_ID, mCursor.getLong(mCursor.getColumnIndex(AlarmProvider.ALARM_ID)));
                data.put(AlarmProvider.ALARM_NAME, mCursor.getString(mCursor.getColumnIndex(AlarmProvider.ALARM_NAME)));
                data.put(AlarmProvider.ALARM_TIME, mCursor.getString(mCursor.getColumnIndex(AlarmProvider.ALARM_TIME)));
                data.put(AlarmProvider.REPEAT_FLAG, mCursor.getInt(mCursor.getColumnIndex(AlarmProvider.REPEAT_FLAG)));
                data.put(AlarmProvider.REPEAT_EXPRESSION, mCursor.getString(mCursor.getColumnIndex(AlarmProvider.REPEAT_EXPRESSION)));
                data.put(AlarmProvider.RING_TYPE, mCursor.getInt(mCursor.getColumnIndex(AlarmProvider.RING_TYPE)));
                data.put(AlarmProvider.ALARM_STATE, mCursor.getInt(mCursor.getColumnIndex(AlarmProvider.ALARM_STATE)));
                data.put(AlarmProvider.RING_SRC, mCursor.getString(mCursor.getColumnIndex(AlarmProvider.RING_SRC)));
                data.put(AlarmProvider.ALARM_TIMEINMILLIS, mCursor.getLong(mCursor.getColumnIndex(AlarmProvider.ALARM_TIMEINMILLIS)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return data;
    }

    /**
     * 添加闹钟
     *
     * @return Alarm Id
     */
    public static long addAlarm(ContentValues values) {
        Uri uriWithAppendedId = getContext().getContentResolver().insert(AlarmProvider.getUri(), values);
        try {
            return Long.parseLong(uriWithAppendedId.getLastPathSegment());
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 修改闹钟
     *
     * @return 响应行数
     */
    public static int updateAlarm(ContentValues values, String where, String[] selectionArgs) {
        return getContext().getContentResolver().update(AlarmProvider.getUri(), values, where, selectionArgs);
    }

    /**
     * 删除闹钟
     *
     * @return 响应行数
     */
    public static int deleteAlarm(String where, String[] selectionArgs) {
        return getContext().getContentResolver().delete(AlarmProvider.getUri(), where, selectionArgs);
    }

    /**
     * 删除所有闹钟
     *
     * @return 响应行数
     */
    public static int dropAlarm() {
        return getContext().getContentResolver().delete(AlarmProvider.getUri(), "1=1", null);
    }

    /**
     * 根据系统电话簿rawContactId查找对应的联系人信息
     *
     * @param rawContactId 对应系统电话簿中的rawContactId字段
     */
    public static ContentValues getContactByRawContactId(long rawContactId) {
        ContentValues result = null;
        String selection = ContactProvider.RAW_CONTACT_ID + "=" + rawContactId;
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(ContactProvider.getUri(), null, selection, null, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                result = new ContentValues();
                result.put(ContactProvider.RAW_CONTACT_ID, mCursor.getLong(mCursor.getColumnIndex(ContactProvider.RAW_CONTACT_ID)));
                result.put(ContactProvider.CONTACT_ID, mCursor.getString(mCursor.getColumnIndex(ContactProvider.CONTACT_ID)));
                result.put(ContactProvider.NICKNAME, mCursor.getString(mCursor.getColumnIndex(ContactProvider.NICKNAME)));
                result.put(ContactProvider.PHONE_NUM, mCursor.getString(mCursor.getColumnIndex(ContactProvider.PHONE_NUM)));
                result.put(ContactProvider.PHOTO_URL, mCursor.getString(mCursor.getColumnIndex(ContactProvider.PHOTO_URL)));
                result.put(ContactProvider.PHOTO_PATH, mCursor.getString(mCursor.getColumnIndex(ContactProvider.PHOTO_PATH)));
                result.put(ContactProvider.TYPE, mCursor.getInt(mCursor.getColumnIndex(ContactProvider.TYPE)));
                result.put(ContactProvider.FRIEND_ID, mCursor.getString(mCursor.getColumnIndex(ContactProvider.FRIEND_ID)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }


    /**
     * 根据id查询对应古兰经挑战设置数据
     *
     * @param id 对应古兰经挑战设置的主键ID
     */
    public static ContentValues getQuranLearnById(int id) {
        ContentValues result = null;
        String selection = QuranLearnProvider.ID + "=" + id;
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(QuranLearnProvider.getUri(), null, selection, null, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                result = new ContentValues();
                result.put(QuranLearnProvider.ID, mCursor.getInt(mCursor.getColumnIndex(QuranLearnProvider.ID)));
                result.put(QuranLearnProvider.CHAPTER_INDEX, mCursor.getInt(mCursor.getColumnIndex(QuranLearnProvider.CHAPTER_INDEX)));
                result.put(QuranLearnProvider.DURATION, mCursor.getInt(mCursor.getColumnIndex(QuranLearnProvider.DURATION)));
                result.put(QuranLearnProvider.DEADLINE, mCursor.getString(mCursor.getColumnIndex(QuranLearnProvider.DEADLINE)));
                result.put(QuranLearnProvider.REWARD_VALUE, mCursor.getInt(mCursor.getColumnIndex(QuranLearnProvider.REWARD_VALUE)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }


    /**
     * 根据friendId查找对应的联系人信息
     */
    public static ContentValues getContactByFriendId(String friendId) {
        ContentValues result = null;
        String selection = ContactProvider.FRIEND_ID + "=?";
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(ContactProvider.getUri(), null, selection, new String[]{friendId}, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                result = new ContentValues();
                result.put(ContactProvider.RAW_CONTACT_ID, mCursor.getLong(mCursor.getColumnIndex(ContactProvider.RAW_CONTACT_ID)));
                result.put(ContactProvider.CONTACT_ID, mCursor.getString(mCursor.getColumnIndex(ContactProvider.CONTACT_ID)));
                result.put(ContactProvider.NICKNAME, mCursor.getString(mCursor.getColumnIndex(ContactProvider.NICKNAME)));
                result.put(ContactProvider.PHONE_NUM, mCursor.getString(mCursor.getColumnIndex(ContactProvider.PHONE_NUM)));
                result.put(ContactProvider.PHOTO_URL, mCursor.getString(mCursor.getColumnIndex(ContactProvider.PHOTO_URL)));
                result.put(ContactProvider.PHOTO_PATH, mCursor.getString(mCursor.getColumnIndex(ContactProvider.PHOTO_PATH)));
                result.put(ContactProvider.TYPE, mCursor.getInt(mCursor.getColumnIndex(ContactProvider.TYPE)));
                result.put(ContactProvider.FRIEND_ID, mCursor.getString(mCursor.getColumnIndex(ContactProvider.FRIEND_ID)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }

    /**
     * 根据rawContactId获取联系人本地头像地址{@link ContactProvider#RAW_CONTACT_ID}
     *
     * @return String {@link ContactProvider#PHOTO_PATH}
     */
    public static String getContactUrlByRawContactId(int rawContactId) {
        String path = "";
        String selection = ContactProvider.RAW_CONTACT_ID + "=" + rawContactId;
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(ContactProvider.getUri(), null, selection, null, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                String photoPath = mCursor.getString(mCursor.getColumnIndex(ContactProvider.PHOTO_URL));
                path = TextUtils.isEmpty(photoPath) ? mCursor.getString(mCursor.getColumnIndex(ContactProvider.PHOTO_PATH)) : photoPath;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return path;
    }


    /**
     * 根据电话号码获取联系人本地头像地址{@link ContactProvider#PHONE_NUM}
     *
     * @return String {@link ContactProvider#PHOTO_PATH}
     */
    public static String getContactPhoto(String phoneNum) {
        String path = "";
        String selection = ContactProvider.PHONE_NUM + "=?";
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(ContactProvider.getUri(), null, selection, new String[]{phoneNum}, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                String photoPath = mCursor.getString(mCursor.getColumnIndex(ContactProvider.PHOTO_PATH));
                path = TextUtils.isEmpty(photoPath) ? mCursor.getString(mCursor.getColumnIndex(ContactProvider.PHOTO_URL)) : photoPath;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return path;
    }

    /**
     * 获取管理员的{@link ContactProvider#FRIEND_ID}
     *
     * @return String {@link ContactProvider#FRIEND_ID}
     */
    public static String getAdmin() {
        String friendId = "";
        String selection = ContactProvider.TYPE + "=1";
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(ContactProvider.getUri(), null, selection, null, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                friendId = mCursor.getString(mCursor.getColumnIndex(ContactProvider.FRIEND_ID));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return friendId;
    }

    /**
     * 查找服务器下发的所有联系人列表
     */
    public static List<ContentValues> getContacts() {
        List<ContentValues> result = null;
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(ContactProvider.getUri(), null, null, null, null);
            if (mCursor != null && mCursor.getCount() > 0) {
                result = new ArrayList<>(mCursor.getCount());
                int index = 0;
                while (mCursor.moveToNext()) {
                    ContentValues data = new ContentValues();
                    data.put(ContactProvider.CONTACT_ID, mCursor.getString(mCursor.getColumnIndex(ContactProvider.CONTACT_ID)));
                    data.put(ContactProvider.RAW_CONTACT_ID, mCursor.getLong(mCursor.getColumnIndex(ContactProvider.RAW_CONTACT_ID)));
                    data.put(ContactProvider.NICKNAME, mCursor.getString(mCursor.getColumnIndex(ContactProvider.NICKNAME)));
                    data.put(ContactProvider.PHONE_NUM, mCursor.getString(mCursor.getColumnIndex(ContactProvider.PHONE_NUM)));
                    data.put(ContactProvider.PHOTO_URL, mCursor.getString(mCursor.getColumnIndex(ContactProvider.PHOTO_URL)));
                    data.put(ContactProvider.PHOTO_PATH, mCursor.getString(mCursor.getColumnIndex(ContactProvider.PHOTO_PATH)));
                    data.put(ContactProvider.TYPE, mCursor.getInt(mCursor.getColumnIndex(ContactProvider.TYPE)));
                    data.put(ContactProvider.FRIEND_ID, mCursor.getString(mCursor.getColumnIndex(ContactProvider.FRIEND_ID)));
                    result.add(index, data);
                    index++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }


    /**
     * 查找服务器下发祈祷设置
     */
    public static ContentValues getPrayerSettingsCustomParams() {
        ContentValues result = null;
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(PrayerSettingsProvider.getUri(), null, null, null, null);
            if (mCursor != null && mCursor.getCount() > 0 && mCursor.moveToNext()) {
                result = new ContentValues();
                result.put(PrayerSettingsProvider.FAJR_ANGLE, mCursor.getDouble(mCursor.getColumnIndex(PrayerSettingsProvider.FAJR_ANGLE)));
                result.put(PrayerSettingsProvider.MAGHRIB_SELECTOR, mCursor.getInt(mCursor.getColumnIndex(PrayerSettingsProvider.MAGHRIB_SELECTOR)));
                result.put(PrayerSettingsProvider.MAGHRIB_VALUE, mCursor.getDouble(mCursor.getColumnIndex(PrayerSettingsProvider.MAGHRIB_VALUE)));
                result.put(PrayerSettingsProvider.ISHA_SELECTOR, mCursor.getInt(mCursor.getColumnIndex(PrayerSettingsProvider.ISHA_SELECTOR)));
                result.put(PrayerSettingsProvider.ISHA_VALUE, mCursor.getDouble(mCursor.getColumnIndex(PrayerSettingsProvider.ISHA_VALUE)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }

    /**
     * 删除所有联系人
     *
     * @return 响应行数
     */
    public static int dropContacts() {
        return getContext().getContentResolver().delete(ContactProvider.getUri(), "1=1", null);
    }

    /**
     * 查找服务器下发的所有语聊好友列表
     */
    public static List<ContentValues> getBuddies() {
        List<ContentValues> result = null;
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(BuddyProvider.getUri(), null, null, null, null);
            if (mCursor != null && mCursor.getCount() > 0) {
                result = new ArrayList<>(mCursor.getCount());
                int index = 0;
                while (mCursor.moveToNext()) {
                    ContentValues data = new ContentValues();
                    data.put(BuddyProvider.FRIEND_ID, mCursor.getString(mCursor.getColumnIndex(BuddyProvider.FRIEND_ID)));
                    data.put(BuddyProvider.NICKNAME, mCursor.getString(mCursor.getColumnIndex(BuddyProvider.NICKNAME)));
                    data.put(BuddyProvider.TYPE, mCursor.getInt(mCursor.getColumnIndex(ContactProvider.TYPE)));
                    data.put(BuddyProvider.AVATAR, mCursor.getString(mCursor.getColumnIndex(BuddyProvider.AVATAR)));
                    data.put(BuddyProvider.CREATE_TIME, mCursor.getLong(mCursor.getColumnIndex(BuddyProvider.CREATE_TIME)));
                    result.add(index, data);
                    index++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }


    /**
     * 查找服务器下发的所有群聊列表
     */
    public static List<ContentValues> getIMGroups() {
        List<ContentValues> result = null;
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(IMGroupProvider.getUri(), null, null, null, null);
            if (mCursor != null && mCursor.getCount() > 0) {
                result = new ArrayList<>(mCursor.getCount());
                int index = 0;
                while (mCursor.moveToNext()) {
                    ContentValues data = new ContentValues();
                    data.put(IMGroupProvider.ID, mCursor.getString(mCursor.getColumnIndex(IMGroupProvider.ID)));
                    data.put(IMGroupProvider.NAME, mCursor.getString(mCursor.getColumnIndex(IMGroupProvider.NAME)));
                    data.put(IMGroupProvider.TYPE, mCursor.getInt(mCursor.getColumnIndex(IMGroupProvider.TYPE)));
                    data.put(IMGroupProvider.AVATAR, mCursor.getString(mCursor.getColumnIndex(IMGroupProvider.AVATAR)));
                    data.put(IMGroupProvider.CREATE_TIME, mCursor.getLong(mCursor.getColumnIndex(IMGroupProvider.CREATE_TIME)));
                    result.add(index, data);
                    index++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }


    public static String getAvatarByFriendId(String friendId) {
        String path = "";
        String[] projection = {BuddyProvider.AVATAR};
        String selection = BuddyProvider.FRIEND_ID + "=?";
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(BuddyProvider.getUri(), projection, selection, new String[]{friendId}, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                path = mCursor.getString(mCursor.getColumnIndex(BuddyProvider.AVATAR));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return path;
    }

    public static String getNickNameByFriendId(String friendId) {
        String name = "";
        String[] projection = {BuddyProvider.NICKNAME};
        String selection = BuddyProvider.FRIEND_ID + "=?";
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(BuddyProvider.getUri(), projection, selection, new String[]{friendId}, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                name = mCursor.getString(mCursor.getColumnIndex(BuddyProvider.NICKNAME));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return name;
    }


    /**
     * 删除所有记步数据
     */
    public static boolean removePedometer() {
        return getContext().getContentResolver().delete(StepCountProvider.getUri(), null, null) > 0;
    }


    /**
     * 获取服务器存储的手表记步信息
     */
    public static ContentValues getStepInfo() {
        ContentValues result = null;
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(StepInfoProvider.getUri(), null, null, null, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                result = new ContentValues();
                result.put(StepInfoProvider.DEVICE_ID, mCursor.getString(mCursor.getColumnIndex(StepInfoProvider.DEVICE_ID)));
                result.put(StepInfoProvider.PERCENT, mCursor.getDouble(mCursor.getColumnIndex(StepInfoProvider.PERCENT)));
                result.put(StepInfoProvider.TOTAL_STEP_VALUE, mCursor.getInt(mCursor.getColumnIndex(StepInfoProvider.TOTAL_STEP_VALUE)));
                result.put(StepInfoProvider.PLAN_STEP_VALUE, mCursor.getInt(mCursor.getColumnIndex(StepInfoProvider.PLAN_STEP_VALUE)));
                result.put(StepInfoProvider.REACH_GOAL_TIMES, mCursor.getInt(mCursor.getColumnIndex(StepInfoProvider.REACH_GOAL_TIMES)));
                result.put(StepInfoProvider.REACH_GOAL, mCursor.getInt(mCursor.getColumnIndex(StepInfoProvider.REACH_GOAL)));
            } else {
                Logger.e("DataProvider", "no data found");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }

    /**
     * 获取手表当天记步数
     */
    public static int getStepsToday() {
        int stepsToday = 0;
        Cursor cursor = null;
        final int todayMillis = DateUtils.getTodayMillis();
        try {
            cursor = getContext().getContentResolver().query(StepCountProvider.getUri(), null, StepCountProvider.STEP_TIME + " = ?", new String[]{String.valueOf(todayMillis)}, "");
            if (cursor != null && cursor.moveToFirst()) {
                stepsToday = cursor.getInt(cursor.getColumnIndex(StepCountProvider.CURRENT_STEPS));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

        return stepsToday;
    }

    /**
     * 获取定位频率设置
     */
    public static ContentValues getLocationPeriod() {
        ContentValues result = null;
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(LocationPeriodProvider.getUri(), null, null, null, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                result = new ContentValues();
                result.put(LocationPeriodProvider.DEVICE_ID, mCursor.getString(mCursor.getColumnIndex(LocationPeriodProvider.DEVICE_ID)));
                result.put(LocationPeriodProvider.TIME_PERIOD, mCursor.getString(mCursor.getColumnIndex(LocationPeriodProvider.TIME_PERIOD)));
                result.put(LocationPeriodProvider.FREQUENCY, mCursor.getInt(mCursor.getColumnIndex(LocationPeriodProvider.FREQUENCY)));
                result.put(LocationPeriodProvider.REPEAT_EXPRESSION, mCursor.getString(mCursor.getColumnIndex(LocationPeriodProvider.REPEAT_EXPRESSION)));
            } else {
                Logger.e("DataProvider", "no data found");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }

    /**
     * 删除所有定位记录
     */
    public static boolean removeAllLocationRecord() {
        return getContext().getContentResolver().delete(LocationRecordProvider.getUri(), null, null) > 0;
    }

    /**
     * 获取所有未接视频电话数目
     */
    public static int getNumOfMissedVideoCalls() {
        int count = 0;
        Cursor mCursor = null;
        try {
            String selection = VideoCallProvider.CALL_TYPE + " IN (" + VideoCallProvider.CallType.VOIP_MISSED_TYPE.getType() + ", " + VideoCallProvider.CallType.VIDEO_MISSED_TYPE.getType() + ") AND " + VideoCallProvider.NEW_CALL + " = 1";
            mCursor = getContext().getContentResolver().query(VideoCallProvider.getUri(), null, selection, null, null);
            if (mCursor != null && mCursor.getCount() > 0) {
                count = mCursor.getCount();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return count;
    }

    /**
     * 根据好友ID获取最后一次视频通话记录
     */
    public static ContentValues getLastVideoCallLogs(String friendId) {
        List<ContentValues> result = getVideoCallLogs(friendId);
        if (result != null && !result.isEmpty()) {
            return result.get(0);
        }
        return null;
    }

    /**
     * 根据好友ID获取所有视频通话记录
     * 结果是根据通话结束时间降序排列
     */
    public static List<ContentValues> getVideoCallLogs(String friendId) {
        List<ContentValues> result = null;
        Cursor mCursor = null;
        try {
            String selection = VideoCallProvider.FRIEND_ID + "=? ";
            String[] selectionArgs = new String[]{friendId};
            String sortOrder = VideoCallProvider.END_TIME + " DESC";
            mCursor = getContext().getContentResolver().query(VideoCallProvider.getUri(), null, selection, selectionArgs, sortOrder);
            if (mCursor != null && mCursor.getCount() > 0) {
                result = new ArrayList<>(mCursor.getCount());
                while (mCursor.moveToNext()) {
                    ContentValues temp = new ContentValues();
                    temp.put(VideoCallProvider.DEVICE_ID, mCursor.getString(mCursor.getColumnIndex(VideoCallProvider.DEVICE_ID)));
                    temp.put(VideoCallProvider.FRIEND_ID, mCursor.getString(mCursor.getColumnIndex(VideoCallProvider.FRIEND_ID)));
                    temp.put(VideoCallProvider.START_TIME, mCursor.getLong(mCursor.getColumnIndex(VideoCallProvider.START_TIME)));
                    temp.put(VideoCallProvider.END_TIME, mCursor.getLong(mCursor.getColumnIndex(VideoCallProvider.END_TIME)));
                    temp.put(VideoCallProvider.CALL_DURATION, mCursor.getLong(mCursor.getColumnIndex(VideoCallProvider.CALL_DURATION)));
                    temp.put(VideoCallProvider.CALL_TYPE, mCursor.getInt(mCursor.getColumnIndex(VideoCallProvider.CALL_TYPE)));
                    temp.put(VideoCallProvider.NEW_CALL, mCursor.getInt(mCursor.getColumnIndex(VideoCallProvider.NEW_CALL)));
                    result.add(temp);
                }
            }
            if (mCursor != null && mCursor.moveToFirst()) {
            } else {
                Logger.e("DataProvider", "no data found");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }


    /**
     * 删除所有挑战任务
     */
    public static boolean deleteAllSportsChallenge() {
        return getContext().getContentResolver().delete(SportsChallengeProvider.getUri(), null, null) > 0;
    }

    /**
     * 删除所有视频通话记录
     */
    public static boolean removeVideoCalllog() {
        return getContext().getContentResolver().delete(VideoCallProvider.getUri(), null, null) > 0;
    }

    public static boolean isPhotoUpload(String imageName) {
        Cursor mCursor = null;
        try {
            String selection = GalleryProvider.IMAGE_NAME + "= ?" + GalleryProvider.TAG1 + "=1 AND " + GalleryProvider.TAG2 + "=1";
            mCursor = getContext().getContentResolver().query(GalleryProvider.getUri(), null, selection, new String[]{imageName}, null);
            if (mCursor != null && mCursor.getCount() > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return false;
    }

    /**
     * 获取已上传到亚马逊，但是未上传给API 服务器的照片列表
     */
    public static List<ContentValues> getNotUploadedPhotos() {
        List<ContentValues> result = null;
        Cursor mCursor = null;
        try {
            String selection = GalleryProvider.TAG1 + "=1 AND " + GalleryProvider.TAG2 + "=0";
            String sortOrder = GalleryProvider.UPLOAD_TIME;
            mCursor = getContext().getContentResolver().query(GalleryProvider.getUri(), null, selection, null, sortOrder);
            if (mCursor != null && mCursor.getCount() > 0) {
                result = new ArrayList<>(mCursor.getCount());
                while (mCursor.moveToNext()) {
                    ContentValues value = new ContentValues();
                    value.put(GalleryProvider.ID, mCursor.getInt(mCursor.getColumnIndex(GalleryProvider.ID)));
                    value.put(GalleryProvider.IMAGE_NAME, mCursor.getString(mCursor.getColumnIndex(GalleryProvider.IMAGE_NAME)));
                    value.put(GalleryProvider.IMAGE_PATH, mCursor.getString(mCursor.getColumnIndex(GalleryProvider.IMAGE_PATH)));
                    value.put(GalleryProvider.IMAGE_SERVER_PATH, mCursor.getString(mCursor.getColumnIndex(GalleryProvider.IMAGE_SERVER_PATH)));
                    value.put(GalleryProvider.IMAGE_SIZE, mCursor.getLong(mCursor.getColumnIndex(GalleryProvider.IMAGE_SIZE)));
                    value.put(GalleryProvider.TAKE_TIME, mCursor.getLong(mCursor.getColumnIndex(GalleryProvider.TAKE_TIME)));
                    value.put(GalleryProvider.UPLOAD_TIME, mCursor.getLong(mCursor.getColumnIndex(GalleryProvider.UPLOAD_TIME)));
                    result.add(value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }


    /**
     * 获取已上传到亚马逊，但是未上传给API 服务器的照片列表
     */
    public static String[] getAllUploadedPhotos() {
        List<String> idsArr = null;
        Cursor mCursor = null;
        try {
            String selection = GalleryProvider.TAG1 + "=1 OR " + GalleryProvider.TAG2 + "=1";
            mCursor = getContext().getContentResolver().query(GalleryProvider.getUri(), null, selection, null, null);
            if (mCursor != null && mCursor.getCount() > 0) {
                idsArr = new ArrayList<>(mCursor.getCount());
                while (mCursor.moveToNext()) {
                    idsArr.add(String.valueOf(mCursor.getInt(mCursor.getColumnIndex(GalleryProvider.ID))));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        if (idsArr != null && idsArr.size() > 0) {
            return idsArr.toArray(new String[0]);
        }
        return null;
    }


    /**
     * 标记相册中的照片已经上传到服务器
     */
    public static int resetAlbumUploadFlag(String[] ids) {
        ContentValues values = new ContentValues();
        values.put(GalleryProvider.TAG1, "0");

        StringBuilder where = new StringBuilder(GalleryProvider.ID + " IN (");
        for (String id : ids) {
            where.append(id).append(",");
        }
        where.deleteCharAt(where.length() - 1);
        where.append(")");
        return getContext().getContentResolver().update(GalleryProvider.getUri(), values, where.toString(), null);
    }

    /**
     * 用于解绑后重置所有已上传的标记 TAG1 和 TAG2 标记为0
     */
    public static int resetAllUploadFlag() {
        String[] ids = getAllUploadedPhotos();
        if (ids == null) {
            return -1;
        }
        ContentValues values = new ContentValues();
        values.put(GalleryProvider.TAG1, "0");
        values.put(GalleryProvider.TAG2, "0");

        StringBuilder where = new StringBuilder(GalleryProvider.ID + " IN (");
        for (String id : ids) {
            where.append(id).append(",");
        }
        where.deleteCharAt(where.length() - 1);
        where.append(")");
        return getContext().getContentResolver().update(GalleryProvider.getUri(), values, where.toString(), null);
    }

    /**
     * 获取所有任务
     */
    public static List<ContentValues> getTasks() {
        List<ContentValues> result = null;
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(TasksProvider.getUri(), null, null, null, null);
            if (mCursor != null && mCursor.getCount() > 0) {
                result = new ArrayList<>(mCursor.getCount());
                int index = 0;
                while (mCursor.moveToNext()) {
                    ContentValues data = new ContentValues();
                    data.put(TasksProvider.ID, mCursor.getInt(mCursor.getColumnIndex(TasksProvider.ID)));
                    data.put(TasksProvider.TASK_ID, mCursor.getInt(mCursor.getColumnIndex(TasksProvider.TASK_ID)));
                    data.put(TasksProvider.TASK_TYPE, mCursor.getString(mCursor.getColumnIndex(TasksProvider.TASK_TYPE)));
                    data.put(TasksProvider.TASK_ACTION, mCursor.getString(mCursor.getColumnIndex(TasksProvider.TASK_ACTION)));
                    data.put(TasksProvider.TASK_TITLE, mCursor.getString(mCursor.getColumnIndex(TasksProvider.TASK_TITLE)));
                    data.put(TasksProvider.TASK_CONTENT, mCursor.getString(mCursor.getColumnIndex(TasksProvider.TASK_CONTENT)));
                    data.put(TasksProvider.TASK_REMINDER_TIME, mCursor.getString(mCursor.getColumnIndex(TasksProvider.TASK_REMINDER_TIME)));
                    data.put(TasksProvider.TASK_REPEAT_EXPRESSION, mCursor.getString(mCursor.getColumnIndex(TasksProvider.TASK_REPEAT_EXPRESSION)));
                    data.put(TasksProvider.TASK_ADDITIONAL_INFO, mCursor.getString(mCursor.getColumnIndex(TasksProvider.TASK_ADDITIONAL_INFO)));
                    result.add(index, data);
                    index++;
                }
                mCursor.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }

    /**
     * 通过群聊id查询所有群员
     */
    public static List<ContentValues> getIMGroupMembersByGroupId(String groupId) {
        List<ContentValues> result = null;
        Cursor mCursor = null;
        try {
            String selection = IMGroupMemberProvider.GROUP_ID + "= ?";
            mCursor = getContext().getContentResolver().query(IMGroupMemberProvider.getUri(), null, selection, new String[]{groupId}, null);
            if (mCursor != null && mCursor.getCount() > 0) {
                result = new ArrayList<>(mCursor.getCount());
                int index = 0;
                while (mCursor.moveToNext()) {
                    ContentValues data = new ContentValues();
                    data.put(IMGroupMemberProvider.MEMBER_ID, mCursor.getString(mCursor.getColumnIndex(IMGroupMemberProvider.MEMBER_ID)));
                    data.put(IMGroupMemberProvider.GROUP_ID, mCursor.getString(mCursor.getColumnIndex(IMGroupMemberProvider.GROUP_ID)));
                    data.put(IMGroupMemberProvider.TYPE, mCursor.getInt(mCursor.getColumnIndex(IMGroupMemberProvider.TYPE)));
                    data.put(IMGroupMemberProvider.NAME, mCursor.getString(mCursor.getColumnIndex(IMGroupMemberProvider.NAME)));
                    data.put(IMGroupMemberProvider.AVATAR, mCursor.getString(mCursor.getColumnIndex(IMGroupMemberProvider.AVATAR)));
                    data.put(IMGroupMemberProvider.AVATAR_INDEX, mCursor.getInt(mCursor.getColumnIndex(IMGroupMemberProvider.AVATAR_INDEX)));
                    data.put(IMGroupMemberProvider.CREATE_TIME, mCursor.getInt(mCursor.getColumnIndex(IMGroupMemberProvider.CREATE_TIME)));
                    result.add(index, data);
                    index++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }


    public static String getGroupAdminIdByGroupId(String groupId) {
        Cursor mCursor = null;
        String adminId = "";
        try {
            String selection = IMGroupMemberProvider.GROUP_ID + "= ? AND " + IMGroupMemberProvider.TYPE + "= 0";
            mCursor = getContext().getContentResolver().query(IMGroupMemberProvider.getUri(), null, selection, new String[]{groupId}, null);
            if (mCursor != null && mCursor.getCount() > 0) {
                while (mCursor.moveToNext()) {
                    adminId = mCursor.getString(mCursor.getColumnIndex(IMGroupMemberProvider.MEMBER_ID));
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return adminId;
    }

    //============================================================================================
    public static String getString(Uri uri, String name) {
        if (TextUtils.isEmpty(name)) {
            return null;
        }
        Cursor mCursor = null;
        try {
            mCursor = getContext().getContentResolver().query(uri, new String[]{name}, null, null, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                return mCursor.getString(mCursor.getColumnIndex(name));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return null;
    }

    public static Integer getInt(Uri uri, String name, int def) {
        String v = getString(uri, name);
        int value;
        try {
            value = v != null ? Integer.parseInt(v) : def;
        } catch (NumberFormatException e) {
            value = def;
        }
        return value;
    }

    public static Integer getCurrentTheme(Uri uri, String name, int def) {
        String v = getString(uri, name);
        return Integer.parseInt(v);
    }

    public static Long getLong(Uri uri, String name, int def) {
        String valString = getString(uri, name);
        long value;
        try {
            value = valString != null ? Long.parseLong(valString) : def;
        } catch (NumberFormatException e) {
            value = def;
        }
        return value;
    }

    public static Double getDouble(Uri uri, String name, double def) {
        String valString = getString(uri, name);
        double value;
        try {
            value = valString != null ? Double.parseDouble(valString) : def;
        } catch (NumberFormatException e) {
            value = def;
        }
        return value;
    }

    public static Boolean getBoolean(Uri uri, String name, boolean def) {
        int val = getInt(uri, name, def ? 1 : 0);
        return val == 1;
    }

    private enum PN {
        /**
         * 所有插件别名
         */
        ALARM("Alarm"), CHAT("Chat"), CONTACTS("Contacts"), LOCATION("Location"), PAIR("Pair"), PEDOMETER("Pedometer"), PUSH("Push"), SOS("SOS"), VIDEOCALL("VideoCall");

        private String name;

        PN(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

    /**
     * 提供服务端设置的设备配置（设备当前状态、开关设置等）ContentProvider
     */
    public static class DeviceConfigProvider {
        public static final String DEVICE_ID = "deviceId";
        public static final String ACTIVE_STATUS = "activeStatus";
        public static final String SIM_TYPE = "simType";
        public static final String CONTRACT_MACHINE = "contractMachine";
        public static final String WHITE_LIST = "whiteList";
        public static final String OFF_ALARM = "offAlarm";
        public static final String SCENE_MODE = "sceneMode";
        public static final String LAUNCHED = "launched";
        public static final String CURRENT_THEME = "theme";
        public static final String FORBIDDEN_SHUTDOWN = "forbiddenShutdown";
        public static final String FORBIDDEN_VIDEO_CALL = "forbiddenVideoCall";
        public static final String IN_FORBIDPERIOD_FLAG = "inForbidPeriodFlag";
        public static final String WALLPAPER = "wallpaper";
        public static final String CUSTOM_WALLPAPER = "customWallpaper";
        public static final String AUTO_ANSWER = "autoAnswer";

        public static final String REPORT_CALL_LOCATION = "reportCallLocation";
        public static final String REPORT_SHUTDOWN_LOCATION = "reportShutdownLocation";
        public static final String LOC_METHOD = "locMethod";
        public static final String CUSTOM_GROUP = "customGroup";
        public static final String QIMI = "qimi";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，设备信息已改变>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return BASE_CONTENT_URI.buildUpon().appendPath("DeviceConfig").build();
        }
    }


    public static class HolderInfoProvider {
        public static final String HOLDER_ID = "holderId";
        public static final String DEVICE_ID = "deviceId";
        public static final String NAME = "name";
        public static final String AVATAR = "avatar";
        public static final String GENDER = "gender";
        public static final String HEIGHT = "height";
        public static final String WEIGHT = "weight";
        public static final String BIRTHDAY = "birthday";
        public static final String STEP_PLAN_VALUE = "stepPlanValue";
        public static final String SPORTS_CHALLENGE_REWARDS = "sportsChallengeRewardsValue";
        public static final String QURAN_LEARN_TOTAL_REWARDVALUE = "quranLearnTotalRewardValue";
        public static final String CHANTS_COUNTER_TOTAL = "chantsCounterTotal";
        public static final String CHANTS_COUNTER_CHALLENGE_TOTAL = "chantsCounterChallengeTotal";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，HolderInfo信息已改变>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return BASE_CONTENT_URI.buildUpon().appendPath("HolderInfo").build();
        }
    }


    /**
     * 提供服务端设置的二维码配置（设备当前二维码、过期时间等）ContentProvider
     */
    public static class DeviceCodeProvider {
        public static final String DEVICE_ID = "deviceId";
        public static final String QR_CODE = "qrCode";
        public static final String BIND_CODE = "bindCode";
        public static final String EXPIRED_TIME = "expiredTime";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，二维码信息已改变>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return BASE_CONTENT_URI.buildUpon().appendPath("DeviceCode").build();
        }
    }

    /**
     * 提供服务端设置的闹钟和本地闹钟ContentProvider
     */
    public static class AlarmProvider {
        public static final String ID = "_id";
        public static final String ALARM_ID = "alarmId";
        public static final String ALARM_NAME = "alarmName";
        public static final String ALARM_TIME = "alarmTime";
        public static final String REPEAT_FLAG = "repeatFlag";
        public static final String REPEAT_EXPRESSION = "repeatExpression";
        public static final String RING_TYPE = "ringType";
        public static final String ALARM_STATE = "state";
        public static final String RING_SRC = "ringSrc";
        public static final String ALARM_TIMEINMILLIS = "alarmTimeInMillis";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，闹钟列表改变>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return BASE_CONTENT_URI.buildUpon().appendPath("Alarm").build();
        }
    }

    /**
     * 祈祷设置ContentProvider
     */
    public static class PrayerSettingsProvider {
        public static final String ID = "_id";
        public static final String NAME = "name";
        public static final String NOTE = "note";
        public static final String FAJR_ANGLE = "fajrAngle";
        public static final String MAGHRIB_SELECTOR = "maghribSelector";
        public static final String MAGHRIB_VALUE = "maghribValue";
        public static final String ISHA_SELECTOR = "ishaSelector";
        public static final String ISHA_VALUE = "ishaValue";
        public static final String INDEX = "prayerIndex";
        public static final String ASRJURISTIC_METHOD = "asrJuristicMethod";
        public static final String AZAN = "azan";
        public static final String PRAYER_REMINDER = "prayerReminder";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，祈祷设置改变>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return BASE_CONTENT_URI.buildUpon().appendPath("PrayerSetting").build();
        }
    }

    /**
     * 古兰经挑战ContentProvider
     */
    public static class QuranLearnProvider {
        public static final String ID = "id";
        public static final String CHAPTER_INDEX = "chapterIndex";
        public static final String DURATION = "duration";
        public static final String DEADLINE = "deadline";
        public static final String REWARD_VALUE = "rewardValue";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，古兰经挑战设置发生改变>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return BASE_CONTENT_URI.buildUpon().appendPath("QuranLearn").build();
        }
    }

    /**
     * 任务ContentProvider
     */
    public static class TasksProvider {
        public static final String ID = "_id";
        public static final String TASK_ID = "id";
        public static final String TASK_TYPE = "type";
        public static final String TASK_ACTION = "t_action";
        public static final String TASK_TITLE = "title";
        public static final String TASK_CONTENT = "content";
        public static final String TASK_REMINDER_TIME = "reminderTime";
        public static final String TASK_REPEAT_EXPRESSION = "repeatExpression";
        public static final String TASK_ADDITIONAL_INFO = "additionalInfo";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，任务设置发生改变>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return BASE_CONTENT_URI.buildUpon().appendPath("Tasks").build();
        }
    }

    /**
     * 提供服务端设置的联系人信息ContentProvider
     */
    public static class ContactProvider {
        public static final String CONTACT_ID = "contactId";
        public static final String RAW_CONTACT_ID = "rawContactId";
        public static final String NICKNAME = "name";
        public static final String PHONE_NUM = "phone";
        public static final String PHOTO_URL = "photoUrl";
        public static final String PHOTO_PATH = "photoPath";
        //0: 管理员增加 1: 管理员 2：APP关注者 3：设备好友
        public static final String TYPE = "type";
        //当type为1，2时表示APP关注账号(邮箱或手机号码)，type为2时表示好友设备号
        public static final String FRIEND_ID = "friendId";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，联系人信息改变>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return BASE_CONTENT_URI.buildUpon().appendPath(ContactInfo.TABLE_NAME).build();
        }
    }

    public static class SettingsProvider {

        public static final String DISTANCE_UNIT = "distanceUnit";
        public static final String SETTINGS_AUTHORITY = "com.umeox.watch.settings";


        public static final Uri SETTINGS_CONTENT_URI = Uri.parse("content://" + SETTINGS_AUTHORITY);

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(SETTINGS_CONTENT_URI, notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            getContext().getContentResolver().notifyChange(SETTINGS_CONTENT_URI, null);
        }

    }

    /**
     * 提供服务端设置的好友信息ContentProvider
     */
    public static class BuddyProvider {
        public static final String FRIEND_ID = "friendId";
        public static final String NICKNAME = "name";
        public static final String AVATAR = "avatar";
        public static final String CREATE_TIME = "createTime";
        //好友类型 0：管理员，1：关注者，2：设备好友，9：群组
        public static final String TYPE = "type";
        public static final String HIDDEN_FEATURES = "hiddenFeatures";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，好友信息改变>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return BASE_CONTENT_URI.buildUpon().appendPath(BuddyInfo.TABLE_NAME).build();
        }
    }

    /**
     * 提供服务端设置的好友信息ContentProvider
     */
    public static class IMGroupProvider {

        public static final String ID = "id";
        public static final String NAME = "name";
        public static final String AVATAR = "avatar";
        public static final String CREATE_TIME = "createTime";
        //0:家庭群聊
        public static final String TYPE = "type";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，群聊信息改变>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return BASE_CONTENT_URI.buildUpon().appendPath(IMGroup.TABLE_NAME).build();
        }
    }

    /**
     * 提供服务端设置的群成员表ContentProvider
     */
    public static class IMGroupMemberProvider {

        public static final String MEMBER_ID = "memberId";
        public static final String GROUP_ID = "groupId";
        public static final String TYPE = "type";
        public static final String NAME = "name";
        public static final String AVATAR = "avatar";
        public static final String AVATAR_INDEX = "avatarIndex";
        public static final String CREATE_TIME = "createTime";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，群成员表信息改变>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return BASE_CONTENT_URI.buildUpon().appendPath("IMGroupMember").build();
        }
    }

    /**
     * 提供服务端设置的频率定位ContentProvider
     */
    public static class LocationPeriodProvider {
        public static final String DEVICE_ID = "deviceId";
        public static final String TIME_PERIOD = "timePeriod";
        public static final String FREQUENCY = "frequency";
        public static final String REPEAT_EXPRESSION = "repeatExpression";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，定位频率设置已改变>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return BASE_CONTENT_URI.buildUpon().appendPath("LocationPeriod").build();
        }
    }

    /**
     * 定位服务提供定位历史记录ContentProvider
     */
    public static class LocationRecordProvider {
        public static final String ID = "id";
        public static final String LAT = "lat";
        public static final String LNG = "lng";
        public static final String ACCURACY = "accuracy";
        public static final String REPORT_TIME = "reportTime";
        public static final String REQUEST_TIME = "requestTime";
        public static final String ADDRESS = "address";
        public static final String REPORT_REASON = "reportReason";
        public static final String REPORT_REASON_ATTR = "reportReasonAttr";
        public static final String LOCATION_MODE = "locationMode";
        public static final String MCC = "mcc";
        public static final String MNC = "mnc";
        public static final String STEP_COUNT = "stepCount";
        public static final String BATTERY_VAL = "batteryVal";
        public static final String CELL_TOWER = "cellTower";
        public static final String NEARBY_CELL_TOWERS = "nearbyCellTowers";
        public static final String WIFI_POINTS = "wifiPoints";
        public static final String NETWORK = "network";
        public static final String LOC_METHOD = "locMethod";
        public static final String REPORT_FLAG = "reportFlag";

        public static final String AUTHORITY = "com.umeox.watch.location";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，位置数据库发生变化>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return Uri.parse("content://" + AUTHORITY).buildUpon().appendPath("LocationRecord").build();
        }
    }

    /**
     * 语聊应用ContentProvider
     */
    public static class ChatContactProvider {
        public static final String FRIEND_ID = "friendId";
        public static final String FRIEND_TYPE = "type";
        public static final String IS_ADMIN = "isAdmin";
        public static final String NAME = "name";
        public static final String AVATAR = "avatar";
        public static final String CREATE_TIME = "createTime";
        public static final String IS_GROUP = "isGroup";
        public static final String LAST_CONTACT_TIME = "lastContactTime";
        public static final String LAST_MESSAGE_ID = "lastMessageId";
        public static final String MISS_COUNT = "missCount";

        public static final String AUTHORITY = "com.umeox.watch.moto.chat";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，语聊数据库发生变化>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return Uri.parse("content://" + AUTHORITY).buildUpon().appendPath("ContactInfo").build();
        }

        public static Uri getUnreadCountUri() {
            return Uri.parse("content://" + AUTHORITY).buildUpon().appendPath("ContactInfo").appendPath("unreadCount").build();
        }
    }

    /**
     * 视频通话应用ContentProvider
     */
    public static class VideoCallProvider {
        public static final String DEVICE_ID = "deviceId";
        public static final String FRIEND_ID = "friendId";
        public static final String START_TIME = "startTime";
        public static final String END_TIME = "endTime";
        public static final String CALL_TYPE = "type";
        public static final String CALL_DURATION = "duration";
        public static final String NEW_CALL = "newCall";
        public static final String AUTHORITY = "com.umeox.watch.moto.videocall";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，视频通话记录变化>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static void notifyChangeWithAppendedPath(String friendId) {
            getContext().getContentResolver().notifyChange(Uri.withAppendedPath(getUri(), friendId), null);
        }

        public static Uri getUri() {
            return Uri.parse("content://" + AUTHORITY).buildUpon().appendPath("CallLog").build();
        }

        public enum CallType {
            /**
             * 视频来电.
             */
            INCOMING_VIDEO_CALL(0),
            /**
             * 语音来电.
             */
            INCOMING_VOIP_CALL(1),
            /**
             * 视频去电
             */
            OUTGOING_VIDEO_CALL(2),
            /**
             * 语音去电
             */
            OUTGOING_VOIP_CALL(3),
            /**
             * 未接视频来电
             */
            VIDEO_MISSED_TYPE(4),
            /**
             * 未接视频来电
             */
            VOIP_MISSED_TYPE(5);

            private final int value;

            CallType(int value) {
                this.value = value;
            }

            public final int getType() {
                return value;
            }
        }
    }

    /**
     * 提供服务端设置的计步信息（目标值、总计步数、当前达标数等）ContentProvider
     */
    public static class StepInfoProvider {
        public static final String DEVICE_ID = "deviceId";
        public static final String PERCENT = "percent";
        public static final String TOTAL_STEP_VALUE = "totalStepValue";
        public static final String PLAN_STEP_VALUE = "planStepValue";
        public static final String REACH_GOAL_TIMES = "reachGoalTimes";
        public static final String REACH_GOAL = "reachGoal";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，计步设置已改变>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return BASE_CONTENT_URI.buildUpon().appendPath("StepInfo").build();
        }
    }


    /**
     * SportsChallengeProvider ContentProvider
     */
    public static class SportsChallengeProvider {
        public static final String ID = "id";
        public static final String SPORTS_TYPE = "sportsType";
        public static final String DURATION = "duration";
        public static final String REMINDER_TIME = "reminderTime";
        public static final String REWARDS_VALUE = "rewardsValue";

        public static final String AUTHORITY = "com.umeox.watch.moto.sportchallenge";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，运动挑战数据已改变>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return Uri.parse("content://" + AUTHORITY).buildUpon().appendPath("today_challenge").build();
        }
    }

    /**
     * 提供服务端设置的手表壁纸ContentProvider
     */
    public static class WallpaperInfoProvider {
        public static final String ID = "_id";
        public static final String URL = "url";
        public static final String PATH = "path";
        public static final String DOWNLOAD = "download";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，壁纸信息有更新>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return BASE_CONTENT_URI.buildUpon().appendPath("WallpaperInfo").build();
        }
    }

    /**
     * 计步应用ContentProvider
     */
    public static class StepCountProvider {
        public static final String CURRENT_STEPS = "stepValue";
        public static final String STEP_TIME = "stepTime";

        public static final String AUTHORITY = "com.umeox.watch.pedometer";

        public static Uri getUri() {
            return Uri.parse("content://" + AUTHORITY).buildUpon().appendPath("Steps").build();
        }
    }

    /**
     * 相册应用ContentProvider
     */
    public static class GalleryProvider {
        public static final String ID = "_id";
        public static final String IMAGE_NAME = "image_name";
        public static final String IMAGE_PATH = "image_path";
        public static final String IMAGE_SERVER_PATH = "image_server_path";
        public static final String IMAGE_SIZE = "image_size";
        public static final String TAKE_TIME = "take_time";
        public static final String UPLOAD_TIME = "upload_time";
        public static final String TAG1 = "tag1";  //标记是否上传到亚马逊上了 0否 1是
        public static final String TAG2 = "tag2";  //标记2

        public static final String AUTHORITY = "com.umeox.watch.moto.galleryProvider";

        public static Uri getUri() {
            return Uri.parse("content://" + AUTHORITY).buildUpon().appendPath("gallery").build();
        }
    }

    /**
     * 根据ID获取故事播放记录
     */
    public static ContentValues getStoryPlayHistoryById(long id) {
        ContentValues result = null;
        Cursor mCursor = null;
        String selection = StoryPlayHistoryProvider.ID + "=?";
        try {
            mCursor = getContext().getContentResolver().query(StoryPlayHistoryProvider.getUri(), null, selection, new String[]{String.valueOf(id)}, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                result = new ContentValues();
                result.put(StoryPlayHistoryProvider.ID, mCursor.getLong(mCursor.getColumnIndex(StoryPlayHistoryProvider.ID)));
                result.put(StoryPlayHistoryProvider.AMOUNT, mCursor.getInt(mCursor.getColumnIndex(StoryPlayHistoryProvider.AMOUNT)));
                result.put(StoryPlayHistoryProvider.IS_UPLOAD, mCursor.getInt(mCursor.getColumnIndex(StoryPlayHistoryProvider.IS_UPLOAD)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }

    /**
     * 保存一条新的故事播放记录
     *
     * @param values
     * @return
     */
    public static boolean saveStoryPlayHistory(ContentValues values) {
        if (!values.containsKey(StoryPlayHistoryProvider.IS_UPLOAD)) {
            values.put(StoryPlayHistoryProvider.IS_UPLOAD, 0);
        }
        return getContext().getContentResolver().insert(StoryPlayHistoryProvider.getUri(), values) != null;
    }

    /**
     * 修改播放记录数量
     *
     * @return 响应行数
     */
    public static int updateStoryPlayHistory(ContentValues values, String where, String[] selectionArgs) {
        return getContext().getContentResolver().update(StoryPlayHistoryProvider.getUri(), values, where, selectionArgs);
    }


    /**
     * 获取固定数量的心率数据
     */
    public static List<ContentValues> getHeartRatesByLimit(int limit) {
        if (limit <= 0) {
            return null;
        }
        List<ContentValues> result = null;
        Cursor mCursor = null;
        String orderBy = HeartRateProvider.MEASURE_TIME + " desc " + "LIMIT " + limit;
        try {
            mCursor = getContext().getContentResolver().query(HeartRateProvider.getUri(), null, null, null, orderBy);
            if (mCursor != null && mCursor.getCount() > 0) {
                result = new ArrayList<>(mCursor.getCount());
                int index = 0;
                while (mCursor.moveToNext()) {
                    ContentValues data = new ContentValues();
                    data.put(HeartRateProvider.ID, mCursor.getLong(mCursor.getColumnIndex(HeartRateProvider.ID)));
                    data.put(HeartRateProvider.MIN_VALUE, mCursor.getInt(mCursor.getColumnIndex(HeartRateProvider.MIN_VALUE)));
                    data.put(HeartRateProvider.MAX_VALUE, mCursor.getInt(mCursor.getColumnIndex(HeartRateProvider.MAX_VALUE)));
                    data.put(HeartRateProvider.AVERAGE_VALUE, mCursor.getInt(mCursor.getColumnIndex(HeartRateProvider.AVERAGE_VALUE)));
                    data.put(HeartRateProvider.MEASURE_TIME, mCursor.getLong(mCursor.getColumnIndex(HeartRateProvider.MEASURE_TIME)));
                    data.put(HeartRateProvider.IS_UPLOAD, mCursor.getInt(mCursor.getColumnIndex(HeartRateProvider.IS_UPLOAD)));
                    result.add(index, data);
                    index++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }
        return result;
    }

    public static boolean saveHeartRate(ContentValues values) {
        if (!values.containsKey(HeartRateProvider.IS_UPLOAD)) {
            values.put(HeartRateProvider.IS_UPLOAD, 0);
        }
        return getContext().getContentResolver().insert(HeartRateProvider.getUri(), values) != null;
    }


    /**
     * 心率ContentProvider
     */
    public static class HeartRateProvider {
        public static final String ID = "_id";
        public static final String MIN_VALUE = "minValue";
        public static final String MAX_VALUE = "maxValue";
        public static final String AVERAGE_VALUE = "averageValue";
        public static final String MEASURE_TIME = "measureTime";
        public static final String IS_UPLOAD = "isUpload";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，心率列表改变>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return BASE_CONTENT_URI.buildUpon().appendPath("HeartRate").build();
        }
    }


    /**
     * 心率ContentProvider
     */
    public static class StoryPlayHistoryProvider {
        public static final String ID = "id";
        public static final String AMOUNT = "amount";
        public static final String IS_UPLOAD = "isUpload";

        public static boolean registerContentObserver(boolean notifyForDescendents, ContentObserver observer) {
            try {
                getContext().getContentResolver().registerContentObserver(getUri(), notifyForDescendents, observer);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void notifyChange() {
            Log.d(TAG, "通知监听程序，故事播放列表改变>>>" + getUri());
            getContext().getContentResolver().notifyChange(getUri(), null);
        }

        public static Uri getUri() {
            return BASE_CONTENT_URI.buildUpon().appendPath("StoryPlayHistory").build();
        }
    }
}
